package com.itboye.sunsun.mycollect.fragments;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.R.integer;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout;

import com.itboye.sunsun.MainActivity;
import com.itboye.sunsun.R;
import com.itboye.sunsun.adapter.CollectGoodsAdapter;
import com.itboye.sunsun.adapter.MyCollectAdapter;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.MyCollectBean;
import com.itboye.sunsun.beans.MyCollectBean.CollectGoodsBean;
import com.itboye.sunsun.beans.PersonDataBean;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.custome.XListView;
import com.itboye.sunsun.login.ui.LoginActivity;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.person.ui.MyCollectActivity.DeleteGoodsListener;
import com.itboye.sunsun.shop.ui.GoodDetailActivity;
import com.itboye.sunsun.support.BaseFragment;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.DebugToast;
import com.itboye.sunsun.utils.IntegerParse;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.CodeErrorException;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

@SuppressLint("ValidFragment")
public class CollectGoodsFragment extends BaseFragment implements OnItemClickListener, DeleteGoodsListener {

	private Context context;
	private XListView xlistview;
	private CollectGoodsAdapter mAdapter;
	private List<CollectGoodsBean> list;
	private View v;
	private List<String> favoriteList;
	private RelativeLayout gonerela;
	private String favourStr="";

	public CollectGoodsFragment(Context context) {
		this.context = context;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		v = inflater.inflate(R.layout.fragment_mycollectgoods, null);
		getCollectGoods();
		initData();
		return v;
	}

	public void initData() {

		xlistview = (XListView) v.findViewById(R.id.xlistview);
		xlistview.setPullLoadEnable(false);
		xlistview.setPullRefreshEnable(false);
		xlistview.setOnItemClickListener(this);

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {

		Intent intent = new Intent(getActivity(), GoodDetailActivity.class);

		intent.putExtra("what", list.get(position - 1).getId());
		startActivity(intent);

	}

	public void getCollectGoods() {
		MyJsonRequest<MyCollectBean> request = new MyJsonRequest.Builder<MyCollectBean>().apiVer("100")
				.typeKey("BY_Product_getFavorites")
				.param("uid", (String) SPUtils.get(App.ctx, null, SPContants.USER_ID, ""))
				.param("page_no", "1")
				.param("page_size", "100").requestListener(new XRequestListener<MyCollectBean>() {

					@Override
					public void onResponse(MyCollectBean response) {
						DebugLog.v("result", response.toString());
						//Toast.makeText(App.ctx, response.getList().get(0).getFavoriteId(), 0).show();
						int count= IntegerParse.parse(response.getCount(), 0);
						
						if(count>0)
						{
							list = response.getList();
							mAdapter = new CollectGoodsAdapter(App.ctx, list);
							xlistview.setAdapter(mAdapter);
							gonerela.setVisibility(View.GONE);
						}else
						{
							list=new ArrayList<>();
							
							gonerela.setVisibility(View.VISIBLE);
						}
						

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						if (exception instanceof CodeErrorException) {
							Toast.makeText(App.ctx, "网络异常", 0).show();
						} else {
							Toast.makeText(App.ctx, "网络异常", 0).show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	@Override
	public void onDeleteCollectGoods() {

		favoriteList = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).isSelected()) {

				favoriteList.add(list.get(i).getId());
				
				
				//如果大于0就执行
				if(list.size()>0)
				{
					
					//如果只有一个
					if(list.size()==1)
					{
						favourStr=favourStr+list.get(i).getFavoriteId();
					}
					//如果有多个
					else if(list.size()>1)
					{
						
					  favourStr=favourStr+list.get(i).getFavoriteId()+",";
					  if(i==list.size()-1)
					  {
						favourStr=favourStr+list.get(i).getFavoriteId();
					  }
					}
				}

			}
		}
		
	//	Toast.makeText(App.ctx, "" + favoriteList.size(), 0).show();
		if(favoriteList.size()>0)
		{
		   deleteCollectGoods(favourStr);
		}

	}

	public void deleteCollectGoods(final String favorStr) {
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>().apiVer("100").typeKey("BY_Favorites_cancel")
				.param("type", 1)
				.param("favorite_id", favorStr).requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						DebugLog.v("result", response.toString());
						// list.remove(location)

						Iterator<CollectGoodsBean> itor = list.iterator();
						while (itor.hasNext()) {
							CollectGoodsBean bean = itor.next();
							if (bean.isSelected()) {
								itor.remove();
							}
						}

						mAdapter.notifyDataSetChanged();
						// Toast.makeText(App.ctx, "" + response, 0).show();

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						if (exception instanceof CodeErrorException) {
						    Toast.makeText(App.ctx, "网络异常", 0).show();
						} else {
							Toast.makeText(App.ctx, "网络异常", 0).show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

}
